package com.nali.dal.mongo.dao;

import com.mongodb.DB;
import com.mongodb.WriteConcern;
import com.nali.dal.dao.IOperateDao;
import com.nali.dal.dao.IOperateDaoFactory;
import com.nali.dal.ds.IDataSource;
import com.nali.dal.ds.UsingDataSource;

public class MongoOperateDaoFactory implements IOperateDaoFactory, UsingDataSource<DB> {
	private DB db;
	private WriteConcern concern;
	
	public MongoOperateDaoFactory() {
		super();
	}

	public MongoOperateDaoFactory(WriteConcern concern, IDataSource<DB> ds) {
		this(concern, ds.getConnection());
	}
	
	public MongoOperateDaoFactory(WriteConcern concern, DB db) {
		super();
		this.concern = concern;
		this.db = db;
	}

	public MongoOperateDaoFactory(DB db) {
		this(WriteConcern.SAFE, db);
	}

	@Override
	public void setDataSource(IDataSource<DB> ds) {
		this.db = ds.getConnection();		
	}

	@Override
	public IOperateDao getDao(String collectionName) {
		return new MongoOperateDao(db.getCollection(collectionName), concern);
	}
}
